<?php 
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
/**
 * User Model
 *
 * @property Funcionario $Funcionario
 */

class User extends AppModel {
	
	public $name = 'User';
	
	public function beforeSave($options = array()) {
    	if (isset($this->data[$this->alias]['password'])) {
        	$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    	}
    	return true;
	}
	
	public $validate = array(
		'username' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'Informe o nome do usuário'
			),
				'rule' => array('between', 5, 20),
				'message' => 'Nome de usuário deve ter entre 5 e 20 caracteres.'
		),
		'password' => array(
			'required' => array(
				'rule' => array('notEmpty'),
				'message' => 'Informe a senha do usuário'
			),
			'rule' => array('between', 5, 15),
			'message' => 'Senhas deve ter entre 5 e 15 caracteres.'
		),
		'role' => array(
				'valid' => array(
						'rule' => array('inList', array('admin', 'author')),
						'message' => 'Please enter a valid role',
						'allowEmpty' => false
				)
			)
		);
	
	/**
	 * belongsTo associations
	 *
	 * @var array
	 */
	public $belongsTo = array(
			'Funcionario' => array(
					'className' => 'Funcionario',
					'foreignKey' => 'funcionario_id',
					'conditions' => '',
					'fields' => '',
					'order' => ''
			)
	);
}